import { useCallback } from "react";
import { useSelector, useDispatch } from "react-redux";
import adminsApi from "../api/admins";
import Creators from "../store/actions";

export const useAdmins = () => {
  const admins = useSelector((state) => state.admins);
  const dispatch = useDispatch();
  const { curPage, eachPage } = admins;
  //获取优惠券数据
  const getAdmins = useCallback(async () => {
    const data = await adminsApi.getAdmins({ curPage, eachPage });
    dispatch(Creators.getAdmins(data));
  }, [curPage, eachPage, dispatch]);
  //修改当前页
  const setAdmCurPage = useCallback(
    (curPage) => {
      dispatch(Creators.setAdmCurPage({ curPage }));
    },
    [dispatch]
  );
  //修改每页条数
  const setAdmEachPage = useCallback(
    (eachPage) => {
      dispatch(Creators.setAdmEachPage({ eachPage }));
    },
    [dispatch]
  );
  return [admins, { getAdmins, setAdmCurPage, setAdmEachPage }];
};
